<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

$db = db_connect();

// 处理删除操作
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
    $id = intval($_GET['id']);
    try {
        $stmt = $db->prepare("DELETE FROM comments WHERE id = ?");
        $stmt->execute([$id]);
        redirect('comments.php', '评论删除成功');
    } catch (Exception $e) {
        redirect('comments.php', '删除失败：' . $e->getMessage(), 'error');
    }
}

// 获取搜索参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$post_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : 0;

// 每页显示的记录数
$per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $per_page;

// 构建查询条件
$where = "1=1";
$params = [];

if ($search) {
    $where .= " AND (c.content LIKE :search OR u.name LIKE :search)";
    $params[':search'] = "%{$search}%";
}

if ($post_id) {
    $where .= " AND c.post_id = :post_id";
    $params[':post_id'] = $post_id;
}

// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM comments c 
              LEFT JOIN users u ON c.user_id = u.id 
              WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);

// 获取评论列表
$sql = "SELECT c.*, u.name as user_name, p.title as post_title 
        FROM comments c 
        LEFT JOIN users u ON c.user_id = u.id 
        LEFT JOIN posts p ON c.post_id = p.id 
        WHERE {$where} 
        ORDER BY c.created_at DESC
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$comments = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>评论管理</h2>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <div class="search-input">
                            <input type="text" name="search" placeholder="搜索评论内容..." 
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <button type="submit" class="btn-search">
                            <i class="fas fa-search"></i>
                        </button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户</th>
                                <th>帖子标题</th>
                                <th>评论内容</th>
                                <th>评论时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($comments as $comment): ?>
                            <tr>
                                <td><?php echo $comment['id']; ?></td>
                                <td><?php echo htmlspecialchars($comment['user_name']); ?></td>
                                <td>
                                    <a href="post_detail.php?id=<?php echo $comment['post_id']; ?>">
                                        <?php echo htmlspecialchars($comment['post_title']); ?>
                                    </a>
                                </td>
                                <td><?php echo htmlspecialchars($comment['content']); ?></td>
                                <td><?php echo date('Y-m-d H:i', strtotime($comment['created_at'])); ?></td>
                                <td>
                                    <a href="javascript:void(0)" 
                                       onclick="deleteComment(<?php echo $comment['id']; ?>)"
                                       class="btn-icon text-danger" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <style>
                .filter-bar {
                    background: #fff;
                    padding: 10px;
                    border-radius: 4px;
                    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
                    margin-bottom: 20px;
                }

                .search-bar {
                    display: flex;
                    margin-top: 12px;
                    align-items: center;
                    gap: 8px;
                }

                .search-input {
                    flex: 1;
                }

                .search-input input {
                    width: 100%;
                    padding: 6px 12px;
                    border: 1px solid #ddd;
                    border-radius: 4px;
                    font-size: 14px;
                    line-height: 1.5;
                    background: #f8f9fa;
                    transition: all 0.3s;
                }

                .search-input input:focus {
                    border-color: #3498db;
                    background: #fff;
                    outline: none;
                }

                .btn-search {
                    padding: 6px 16px;
                    background: #3498db;
                    color: #fff;
                    border: none;
                    border-radius: 4px;
                    cursor: pointer;
                    line-height: 1.5;
                    transition: background 0.3s;
                }

                .btn-search:hover {
                    background: #2980b9;
                }

                /* 响应式布局 */
                @media (max-width: 768px) {
                    .search-bar {
                        flex-direction: column;
                    }
                    
                    .search-input,
                    .btn-search {
                        width: 100%;
                    }
                }
                </style>

                <!-- 添加分页 -->
                <?php 
                echo generate_pagination($page, $total_pages, [
                    'search' => $search,
                    'post_id' => $post_id
                ]); 
                ?>
            </div>
        </div>
    </div>

    <script>
    function deleteComment(id) {
        if (confirm('确定要删除这条评论吗？')) {
            window.location.href = `comments.php?action=delete&id=${id}`;
        }
    }
    </script>
</body>
</html> 